﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScriptCoreLib.JavaScript.DOM.HTML;
using ScriptCoreLib.JavaScript.Extensions;
using ScriptCoreLib.JavaScript.DOM;
using ScriptCoreLib.JavaScript;
using ScriptCoreLib.JavaScript.Runtime;
using ScriptCoreLib.Shared.Drawing;

namespace OrcasUltraWebApplication2
{
	public sealed class UltraApplication
	{
		public UltraApplication(IHTMLElement e)
		{
			var current = new IHTMLDiv();

			current.style.color = Color.Red;
			current.innerText = "none";

			StringAction UltraApplication_set_current =
				value =>
				{
					current.innerText = value;
				};



			current.AttachToDocument();

			Expando.Of(Native.Window).SetMember("UltraApplication_set_current", IFunction.OfDelegate(UltraApplication_set_current));


			var c = new IHTMLDiv
			{
				innerHTML = @"

           <h1>Picking</h1>
Click on an object
<br/>
<!-- Start of O3D plugin -->
<div id='o3d' style='width: 600px; height: 400px;'></div>
<!-- End of O3D plugin -->
<div style='font-family: sans-serif; font-size: large;'>PICKED: <span id='pickInfo'></span></div>

"
			};

			c.AttachToDocument();

			Load(
					new string[]
							{
						"o3djs/base.js",
						"o3djs/util.js", 
						"o3djs/texture.js", 
						"o3djs/io.js", 
						
						"o3djs/event.js", 
						"o3djs/error.js", 

						"o3djs/math.js",
						"o3djs/dump.js",
						"o3djs/picking.js",
						"o3djs/camera.js",

						"o3djs/primitives.js",
						"o3djs/debug.js",
						
						"o3djs/quaternions.js", 
						"o3djs/rendergraph.js", 
						"o3djs/pack.js", 
						"o3djs/material.js", 
						"o3djs/effect.js", 
						"o3djs/shape.js", 
						"o3djs/element.js", 
						"o3djs/arcball.js", 
						"o3djs/scene.js", 
						"o3djs/serialization.js"
					},
					delegate
					{
						var s2 = new IHTMLScript { type = "text/javascript", src = "Picking.js" };

						s2.onload +=
							delegate
							{
								var init = new IFunction("return init();");
								var unload = new IFunction("return unload();");

								new Timer(
									t =>
									{
										//Debugger.Break();

										init.apply(Native.Window);

										Native.Window.onbeforeunload +=
											delegate
											{
												unload.apply(Native.Window);
											};

										t.Stop();
									}
								, 1, 1);

							};

						s2.AttachToDocument();

					}
				);

		}

		public static void Load(string[] e, Action done)
		{
			var i = 0;

			var LoadCurrent = default(Action);

			LoadCurrent = delegate
			{
				Native.Document.title = e[i];

				var s2 = new IHTMLScript { type = "text/javascript", src = e[i] };

				s2.onload +=
					delegate
					{
						i++;

						if (i == e.Length)
							done();
						else
							LoadCurrent();

					};

				s2.AttachToDocument();
			};

			LoadCurrent();
		}

	}

	public delegate void StringAction(string e);

	public sealed class UltraWebService
	{
		public void GetTime(string x, StringAction result)
		{
			result(x + DateTime.Now);
		}
	}

}
